#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 crane <crane@crane-pc>
#
# Distributed under terms of the MIT license.

import matplotlib
import matplotlib.pyplot as plt
from numpy import arange
from math import *


# plt.xlim(-3, 3)
# plt.ylim(-3, 3)
plt.gca().set_aspect('equal', adjustable='box')


def polar_2_xy(r, s):
    ''' '''
    x = r * cos(s)
    y = r * sin(s)
    return x,y

def plot_x_y_axis(max=200):
    a =  [-max, max]
    b =  [0, 0]

    plt.plot(a, b)     # x
    plt.plot(b, a)     # y


width = 0.01
def make_polar_arrow(plt, theta1, func):
    # print(locals())
    delta = 0.002
    theta2 = theta1 + delta

    r1 = func(theta1)
    r2 = func(theta2)

    x1, y1 = polar_2_xy(r1, theta1)
    x2, y2 = polar_2_xy(r2, theta2)

    global width
    width += 0.03

    plt.arrow(
        x1, y1,
        x2-x1, y2-y1,
        # head_width=0.02, head_length=0.1)
        head_width=width, head_length=0.1)

def draw_rose():
    '''
        n: 画几圈
        ratio : r/theta (阿基米德螺旋线极坐标 半径/弧度)
        start : 初始点距离x距离
    '''
    xs = []
    ys = []

    l = lambda theta: sin(2*theta)
    for degree in arange(0, 2*pi, 0.01):
        # r = sin(2*degree)
        r = l(degree)
        x, y = polar_2_xy(r, degree)

        xs.append(x)
        ys.append(y)

    plt.plot(xs, ys)
    plot_x_y_axis(2)

    # 画 arrow
    for theta in arange(0, 2*pi, 2*pi/9):
        # print('one')
        # make_polar_arrow(plt, theta, lambda theta: sin(2*theta))
        print(theta)
        make_polar_arrow(plt, theta, l)

    # plt.arrow(
    #     1, 1,
    #     0.1, 0.1,
    #     head_width=0.2, head_length=0.1)

    plt.show()

def main():
    print("start main")
    draw_rose()

if __name__ == "__main__":
    main()
